////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CPlatform.h
//
//		Author			:		Jayson Wu
//
//		Purpose			:		A header for all functions and members of the Platform class
//
//		Dates			:		Written( March 2, 2011 ) by Jayson Wu		LastUpdate( March 2, 2011  ) by Jayson Wu 
//	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CPlatform.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "VIN_ObjectManager.h"
#include "CPlayer.h"


CPlatform::CPlatform()
{
	SetType(OBJ_PLATFORM);
	// Use "PLAT_DROP" for Platforms that you can drop down from
	SetPlatformType(PLAT_SOLID);
}

CPlatform::~CPlatform()
{
}


void CPlatform::Render()
{
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pTM->Draw( GetImageID(), (int)GetPosX(), (int)GetPosY(), (float)((float)GetWidth()/(float)pTM->GetTextureWidth(GetImageID())), (float)((float)GetHeight()/(float)pTM->GetTextureHeight(GetImageID())));

	RECT Collision;
	SetRect( &Collision, (int)GetPosX(), (int)GetPosY(), (int)(GetPosX() + GetWidth()), (int)(GetPosY() + GetHeight()) );

	CSGD_Direct3D::GetInstance()->DrawRect( Collision, 255, 0, 0);
}


void CPlatform::Update( float fElapsedTime )
{
	CBaseObject::Update(fElapsedTime);
}

RECT CPlatform::GetRect()
{
	RECT CollisionRect;
	SetRect(&CollisionRect, (int)GetPosX(), (int)GetPosY(), (int)(GetPosX() + GetWidth()), (int)(GetPosY() + GetHeight()));
	return CollisionRect;
}


bool CPlatform::CheckCollision( IBaseInterface* pBase )
{
	RECT TempRect;

	if( pBase->GetType() == OBJ_PLAYER )
	{	
		CPlayer* pPlayer = (CPlayer*)pBase;

		if(IntersectRect(&TempRect, &GetRect(), &pPlayer->GetHealthRect() ) )
		{


			// If width is greater than height, then it is a Top/Bottom collision
			if(TempRect.bottom - TempRect.top < TempRect.right - TempRect.left)
			{
				// Top Of ledge Collision
				if(pPlayer->GetPosY() < GetPosY())
				{
					pPlayer->SetPosY(GetPosY() - pPlayer->GetHeight() - 1);
					pPlayer->SetVelY(1);
					pPlayer->Jumpy();
				}

				//Bottom of ledge Collision
				if(pPlayer->GetPosY() > GetPosY())
				{
					pPlayer->SetVelY(0);
					pPlayer->SetPosY(GetPosY() + GetHeight() + 1);
				}
			}

			// If Height is greater than Width, then it is a Left/Right collision
			if(TempRect.bottom - TempRect.top > TempRect.right - TempRect.left)
			{
				// Left Side Collision
				if(pPlayer->GetPosX() < GetPosX())
				{
					pPlayer->SetPosX(GetPosX() - pPlayer->GetWidth() - 1);
					pPlayer->SetVelX(0);	
				}
				// Right Side Collision
				if(pPlayer->GetPosX() > GetPosX())
				{
					pPlayer->SetPosX((GetPosX() + GetWidth()) + 1);
					pPlayer->SetVelX(0);
				}
			}
		}
	}
	return false;
}
